package com.conselheiro.web.persistence.webdao;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import com.conselheiro.web.entidade.Usuario;

public class UsuarioWebDAO extends WebDAO<Usuario> {

	@Override
	protected Class<Usuario> getClasse() {
		return Usuario.class;
	}

	public Usuario getUsuario(String login, String senha) {
		EntityManager entityManager = getEntityManager();
		try {
			Query query = entityManager.createQuery("SELECT o FROM "
					+ getClasse().getSimpleName()
					+ " o WHERE login LIKE :login AND senha LIKE :senha");
			query.setParameter("login", login).setParameter("senha", senha);
			return (Usuario) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		} finally {
			entityManager.close();
		}
	}
	
	public Usuario getCadastro (String senha, String nome, String sobrenome, String email ) {
		
					
		EntityManager entityManager = getEntityManager();
		try {
			Query query = entityManager.createQuery("INSERT INTO usuario "
					+ getClasse().getSimpleName());
			query.setParameter("login", nome).setParameter("senha", senha).setParameter("dataNascimenti", ""). setParameter("emailSecundario", email). setParameter("genero", ""). setParameter("nome", nome);
			return (Usuario) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		} finally {
			entityManager.close();
		}
	}

	public Usuario recuperarUsuario(String login) {
		EntityManager entityManager = getEntityManager();
		try {
			Query query = entityManager.createQuery("SELECT o FROM "
					+ getClasse().getSimpleName()
					+ " o WHERE login LIKE :login");
			query.setParameter("login", login);
			return (Usuario) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		} finally {
			entityManager.close();
		}
	}

}
